package sys

import (
	"context"
	"fmt"
	"gitee.com/freelifer/module-admin/model"
	"gitee.com/freelifer/module-admin/services/sys"
	"gitee.com/freelifer/module-web-base/request"
	"github.com/go-spring/spring-base/util"
	"github.com/go-spring/spring-core/gs"
	"github.com/go-spring/spring-core/web"
	"net/http"
)

var (
	ApiVerify = request.Rules{"Path": {request.NotEmpty()}, "Description": {request.NotEmpty()}, "ApiGroup": {request.NotEmpty()}, "Method": {request.NotEmpty()}}
)

func init() {
	// SpringBoot.RegisterBean(new(Service)).AsInterface((*types.FileProvider)(nil))
	gs.Object(new(APIController)).Init(func(c *APIController) {

		///sys/admin/apis
		gs.GetMapping("/sys/admin/apis", c.GetApis)

		// 系统api的创建
		gs.PostBinding("/sys/admin/apis", c.CreateApi)
		gs.GetMapping("/api/user", c.GetUser)
	})
}

type APIController struct {
	APIService *sys.APIService `autowire:""`
}

// 用户用户信息
func (c *APIController) GetApis(webCtx web.Context) {
	err, apis := c.APIService.GetAllApis()
	if err != nil {
		webCtx.JSON(err.Error())
		return
	}

	webCtx.JSON(apis)
}

func (c *APIController) CreateApi(ctx context.Context, api *model.SysApi) string {
	err := request.Verify(*api, ApiVerify)
	util.Panic(err).When(err != nil)

	err = c.APIService.CreateApi(*api)
	util.Panic(err).When(err != nil)

	return "创建失败"
}

func (c *APIController) PostUser(ctx context.Context, req *userBind) string {
	return ""
}

// 用户用户信息
func (c *APIController) GetUser(webCtx web.Context) {
	fmt.Println("xxxx..x.x..x.x.x.")
	webCtx.SetStatus(http.StatusNotFound)
	webCtx.String("hello world")
}
